package com.ndd.config;

import com.ndd.interceptor.TokenInterceptorHandler;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @ProjectName: cmall-ybu
 * @Titile: TokenHandlerConfig
 * @Author: Lucky
 * @Description: 注册token拦截器
 */
@SpringBootConfiguration //配置类
public class Webconfig implements WebMvcConfigurer {
    /**
     * 设置拦截资源的路径和放行资源路径
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new TokenInterceptorHandler())
                .addPathPatterns("/api/**") //拦截url
                .excludePathPatterns("/api/user/login","/api/user/reg")// 放行注册和登录
                .excludePathPatterns("/api/product/**","api/category/**");//放行商品相关
    }

    /**
     * 设置跨域问题
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")//所有的请求都允许跨域
                .allowedOriginPatterns("*")//设置允许所有来源的跨域请求
                .allowedMethods("GET", "POST", "PUT", "DELETE")
                .allowedHeaders("*")//允许所有的请求头
                .allowCredentials(true)
                .maxAge(3600);//预检请求的有效期，单位秒
    }
}
